package cn.shop.dys.activity;

import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import cn.shop.dys.adapter.SearchAdapter;
import cn.shop.lib_common.Cheeses;
import cn.shop.dys.R;

/**
 *
 * 搜索
 * Created by sunjian on 2016/4/18.
 *
 */
/**
 * 暂时不用
 * 作者:孙建
 * created at 2016/4/21 19:11
 */
public class SearchActivity extends AppCompatActivity {
   @ViewInject(R.id.tb_search)
      private Toolbar mToolbar;
    @ViewInject(R.id.ll_search_empty)
    private LinearLayout mSearchEmpty;
    @ViewInject(R.id.lv_search_list)
    private ListView mSearchList;
    private SearchView mSearchView;
    private SearchAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ViewUtils.inject(this);
        init();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.main, menu);
        initListView();
        initSearchView(menu);
        /*try {
            Field field = mSearchView.getClass().getDeclaredField("mSubmitButton");
            field.setAccessible(true);
            ImageView iv = (ImageView) field.get(mSearchView);
            iv.setImageDrawable(this.getResources().getDrawable(R.drawable.ic_menu_emoticons));
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        return true;
    }

    private void initSearchView(Menu menu) {
        MenuItem search = menu.findItem(R.id.menu_search);
        search.collapseActionView();
        search.expandActionView();
        mSearchView= (SearchView) search.getActionView();
        //   mSearchView.setIconifiedByDefault(false);
     //   mSearchView.setSubmitButtonEnabled(true);
        mSearchView.setQueryHint("搜索");
        MenuItemCompat.setOnActionExpandListener(search, new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                finish();
                return true;
            }

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                return true;
            }
        });
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
               /* if (!TextUtils.isEmpty(newText)) {
                    mSearchEmpty.setVisibility(View.INVISIBLE);
                    mSearchList.setVisibility(View.VISIBLE);
                    mSearchList.setFilterText(newText);
                } else {
                    mSearchList.clearTextFilter();
                    mSearchList.setVisibility(View.INVISIBLE);
                    mSearchEmpty.setVisibility(View.VISIBLE);
                }*/
                if (mAdapter instanceof Filterable) {
                    Filter filter = mAdapter.getFilter();
                    if (TextUtils.isEmpty(newText)) {
                        filter.filter(null);
                        mSearchList.setVisibility(View.INVISIBLE);
                        mSearchEmpty.setVisibility(View.VISIBLE);
                    } else {
                        mSearchEmpty.setVisibility(View.INVISIBLE);
                        mSearchList.setVisibility(View.VISIBLE);
                        filter.filter(newText);
                    }
                }
                return true;
            }
        });
      /*  mSearchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg= (String) mSearchView.getQuery();
                CommonUtil.showToast(SearchActivity.this,msg);
            }
        });*/
       /* mSearchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
            @Override
            public boolean onSuggestionSelect(int position) {
                String msg= (String) mSearchView.getQuery();
                CommonUtil.showToast(SearchActivity.this, msg);
                return false;
            }

            @Override
            public boolean onSuggestionClick(int position) {
                String msg= (String) mSearchView.getQuery();
                CommonUtil.showToast(SearchActivity.this,msg);
                return false;
            }
        });*/
      //  mSearchView.setQuery("张",false);
    }


    private void init() {
        initToolbar();
    }

    private void initListView() {
        if(mAdapter==null){
            List<String> infos=new ArrayList<>();
            for (int i = 0; i < Cheeses.NAMES.length; i++) {
                infos.add(Cheeses.NAMES[i]);
            }
        //    mSearchList.setEmptyView(findViewById(R.id.ll_search_empty));
            mAdapter=new SearchAdapter(this,infos);
            mSearchList.setAdapter(mAdapter);
            mSearchList.setTextFilterEnabled(true);
        }else {
            mAdapter.notifyDataSetChanged();
        }
        mSearchList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }

    private void initToolbar() {
       mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
    //     mSearchView = (SearchView) mToolbar.getMenu().findItem(R.id.menu_search).getActionView();
        /*mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_search:
                        item.collapseActionView();
                        SearchView searchview = (SearchView) item.getActionView();
                        CommonUtil.showSafeToast(SearchActivity.this, "搜索");
                        break;
                }
                return false;
            }
        });*/
    }
}
